import subprocess
import IPy
ip = input("请输入要检查的网段、子网掩码")
try:
    ips = IPy.IP(ip)
    print(ips)
    for i in ips:
        result = subprocess.getoutput(f"ping -n 1 -w 1 {i}")
        if "请求超时" in result:
            continue
        else:
            ip_result = str(i) + "已被使用"
            with open("ip_result.txt", "a+", encoding="utf-8") as f:
                f.write(ip_result + "\n")
    print("执行成功")
except ValueError as ve:
    print("输入的ip地址有误")
